package com.example.pdftoofd.controller;

import com.example.pdftoofd.result.BaseResult;
import com.example.pdftoofd.service.PDFConvertOFDService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.Objects;

@Api(tags = "pdf转ofd管理")
@RestController
@RequestMapping("/pdf")
public class ConvertFileController {
    @Autowired
    private PDFConvertOFDService pdfConvertOFDService;

    @ApiOperation("pdf转ofd接口")
    @PostMapping("/convert/ofd")
    public String pdfToOFD(@RequestParam(required = false) MultipartFile file, @RequestParam(required = false) String MD5, HttpServletResponse response) throws IOException {
        if (Objects.isNull(file)) {
            throw new IOException("没有上传文件!");
        }
        if (StringUtils.isNotEmpty(MD5)) {
            if (!MD5.equals(DigestUtils.md5Hex(file.getInputStream()))) {
                throw new IOException("MD5签名不一致!");
            }
        }

        String execute = pdfConvertOFDService.execute(file);
        File OFDFile = new File(execute);
        // 设置响应头、以附件形式打开文件
        response.setHeader("content-disposition", "attachment; fileName=" + OFDFile.getName());
        try (InputStream inputStream = new FileInputStream(OFDFile);
             ServletOutputStream outputStream = response.getOutputStream()) {
            int len = 0;
            byte[] data = new byte[1024];
            while ((len = inputStream.read(data)) != -1) {
                outputStream.write(data, 0, len);
            }
        } catch (IOException e) {
            throw new IOException(e);
        }
        return execute;
    }

    /*public ResponseEntity<InputStreamResource> down2(@RequestParam(required = false) MultipartFile file, @RequestParam(required = false) String MD5, @RequestParam(required = false) String base64, HttpServletResponse response) throws Exception {
        BaseResult execute = pdfConvertOFDService.execute(file);
        String message = execute.getMessage();
        File OFDFile = new File(message);
        // 设置响应头、以附件形式打开文件
        response.setHeader("content-disposition", "attachment; fileName=" + OFDFile.getName());
        InputStream inputStream = new FileInputStream(OFDFile);
        //获得输出流对象
        ServletOutputStream outputStream = response.getOutputStream();
        int len = 0;
        byte[] data = new byte[1024];
        while ((len = inputStream.read(data)) != -1) {
            outputStream.write(data, 0, len);
        }
        outputStream.close();
        inputStream.close();
        InputStreamResource isr = new InputStreamResource(new FileInputStream("E:\\desktop\\1.png"));
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=test1.png")
                .body(isr);
    }*/
}
